package com.gopay.solution.manager.impl;

import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.gopay.common.exception.validator.GopayCheckException;
import com.gopay.common.util.DateUtils;
import com.gopay.common.constants.solution.SolutionTypeConstant;
import com.gopay.solution.dao.SsApSolutionRelDAO;
import com.gopay.solution.domain.po.SolutionRuleDtl;
import com.gopay.solution.domain.po.SsApSolutionRel;
import com.gopay.solution.domain.vo.Solution;
import com.gopay.solution.manager.SolutionManager;
import com.gopay.solution.manager.SsLoSolutionManager;

@Service(value = "ssLoSolutionManager")
public class SsLoSolutionManagerImpl implements SsLoSolutionManager {

    @Resource(name="ssApSolutionRelDAO")
    private SsApSolutionRelDAO ssApSolutionRelDAO;
    
    @Resource(name = "ssTransLegalObjSoluManager")
    private SolutionManager ssTransLegalObjSoluManager;
    
    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = { Exception.class, RuntimeException.class })
    public void saveSsTbSolution(Solution solu, String acctProdCd, String gopayIntTxnCd) throws GopayCheckException {
        if(StringUtils.isEmpty(acctProdCd) || StringUtils.isEmpty(gopayIntTxnCd) || solu == null) {
            throw new GopayCheckException("", "传入数据有误，操作失败");
        }
        
        //----------------先保存关系
        SsApSolutionRel rel = ssApSolutionRelDAO.getByApIdAndSoluType(acctProdCd, SolutionTypeConstant.TRANS_LEGALOBJ);
        if(rel == null) {
            rel = new SsApSolutionRel();
            rel.setSq(System.currentTimeMillis());
            rel.setProdCode(acctProdCd);
            rel.setSolutionId(solu.getMeta().getSolutionId());
            rel.setSolutionType(SolutionTypeConstant.TRANS_LEGALOBJ);
            rel.setCreatedTs(DateUtils.getSystemDate());
            rel.setLastUpdTs(DateUtils.getSystemDate());
            rel.setLastUpdTranCode("");
            
            //保存关系
            ssApSolutionRelDAO.save(rel);
        }
                
        
        //--------------------再保存方案
        Solution local = ssTransLegalObjSoluManager.load(solu.getMeta().getSolutionId());
        //如果local==null,表示该方案不存在，需要新建方案和方案与账户产品的关系。 
        if(local == null) {
            //保存方案
            ssTransLegalObjSoluManager.save(solu);
        }
        else {
            //如果不为空，表示该方案和方案与账户产品的关系已存在，只需要保存本次传入的方案明细即可
            local.setRuleDtls(solu.getRuleDtls());
            ssTransLegalObjSoluManager.modifyBySeqAndIntTxnCd(local, gopayIntTxnCd);
        }

    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = { Exception.class, RuntimeException.class })
    public void editSsTbSolution(long solutionId, String gopayIntTxnCd, List<SolutionRuleDtl> rules)
            throws GopayCheckException {
        
        if(solutionId < 0 || StringUtils.isEmpty(gopayIntTxnCd) || rules == null) {
            throw new GopayCheckException("", "传入数据有误，操作失败");
        }
        
        Solution solu = ssTransLegalObjSoluManager.loadBySeqAndIntTxnCd(solutionId, gopayIntTxnCd);
        if(solu == null) {
            throw new GopayCheckException("", "关键信息丢失，操作失败");
        }
        
        solu.setRuleDtls(rules);
        
        boolean ret = ssTransLegalObjSoluManager.modifyBySeqAndIntTxnCd(solu, gopayIntTxnCd);
        if(!ret) {
            throw new GopayCheckException("", "修改合法交易对方方案操作失败");
        }

    }

}
